package Test1;

import java.util.Arrays;

public class Test {

    public static void func(int[]p,int n1,int n2) {
        int left=n1;
        int right=n2-1;
        while (left<right) {
            int tmp=p[left];
            p[left]=p[right];
            p[right]=tmp;
            left++;
            right--;
        }
    }
    public static void rotate(int[] nums, int k) {
        //翻转数组
        func(nums,0,nums.length-k);
        func(nums,nums.length-k,nums.length);
        func(nums,0,nums.length);
        System.out.println(Arrays.toString(nums));

        //三步翻转法
    }
    public static int search(int[] arr, int target) {
        if (arr == null)
            return -1;
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                while(mid>1&&arr[mid]==arr[mid-1]) {
                    mid--;
                }
                return mid;
            }
            //左半部分有序
            else if (arr[0] < arr[mid]) {
                //当target落在有序区域
                if (target < arr[mid]&&target>=arr[left]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else if(arr[mid]<arr[right]) {
                if (target > arr[mid]&&target<=arr[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            else {
                left++;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int []arr={1,2,3,4,5,6,7};
        //[5, 4, 3, 5, 2, 5, 5, 1]
        rotate(arr,3);
        System.out.println(search(arr, 5));
    }
}
